package ec.atreasure.com.core.net;

import android.content.Context;

import java.io.File;
import java.util.WeakHashMap;

import ec.atreasure.com.core.download.DownloadHandler;
import ec.atreasure.com.core.net.callback.IError;
import ec.atreasure.com.core.net.callback.IFailure;
import ec.atreasure.com.core.net.callback.IRequest;
import ec.atreasure.com.core.net.callback.ISuccess;
import ec.atreasure.com.core.net.callback.RequestCallbacks;
import ec.atreasure.com.core.ui.AtreasureLoader;
import ec.atreasure.com.core.ui.LoaderStyle;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * Description :基于refrofit网络请求实现类
 * Created by wangjin on 2019/6/22.
 * Job number：
 * Phone ：18301070822
 * Email： 120182051@qq.com
 * Person in charge : 汪渝栋
 * Leader：
 */
public class RestClient {

    //请求参数
    private final WeakHashMap<String, Object> mParams;

    //请求地址
    private final String url;

    //请求开始/结束回调
    private final IRequest requestCallback;

    //请求错误回调
    private final IError errorCallback;

    //请求失败回调
    private final IFailure failureCallback;

    //请求成功回调
    private final ISuccess successCallback;

    //put raw,post raw请求体
    private final RequestBody body;

    //过度动画样式
    private final LoaderStyle loaderStyle;

    //显示上下文具体到fragment或者activity
    private final Context context;

    //上传文件
    private final File uploadFile;

    //下载文件目录
    private final String downloadDir;

    //下载文件扩展名
    private final String extension;

    //下载文件名
    private final String downloadFileName;


    RestClient(WeakHashMap<String, Object> mParams,
               String url, IRequest requestCallback,
               IError errorCallback, IFailure failureCallback,
               ISuccess successCallback, RequestBody body,
               LoaderStyle loaderStyle, File uploadFile,
               String downloadDir, String extension,
               String downloadFileName, Context context) {
        this.mParams = mParams;
        this.url = url;
        this.requestCallback = requestCallback;
        this.errorCallback = errorCallback;
        this.failureCallback = failureCallback;
        this.successCallback = successCallback;
        this.body = body;
        this.loaderStyle = loaderStyle;
        this.uploadFile = uploadFile;
        this.context = context;
        this.downloadDir = downloadDir;
        this.extension = extension;
        this.downloadFileName = downloadFileName;
    }

    /**
     * 获取RestClientBuilder实例
     *
     * @return
     */
    public static RestClientBuilder getRestClientBuilder() {
        return new RestClientBuilder();
    }

    /**
     * 发起get请求
     */
    public final void get() {
        request(HttpMethod.GET);
    }

    /**
     * 发起post/post raw请求
     */
    public final void post() {
        if (body == null) {
            request(HttpMethod.POST);
        } else {
            if (!mParams.isEmpty()) {
                throw new RuntimeException("params is must be null!");
            }
            request(HttpMethod.POST_RAW);
        }
    }

    /**
     * 发起put/put raw请求
     */
    public final void put() {
        if (body == null) {
            request(HttpMethod.PUT);
        } else {
            if (!mParams.isEmpty()) {
                throw new RuntimeException("params is must be null!");
            }
            request(HttpMethod.PUT_RAW);
        }
    }

    /**
     * 发起delete请求
     */
    public final void delete() {
        request(HttpMethod.DELETE);
    }

    /**
     * 发起下载文件请求
     */
    public final void download() {
        new DownloadHandler(mParams, url, requestCallback, errorCallback, failureCallback, successCallback, downloadDir, extension, downloadFileName)
                .startDownload();
    }

    /**
     * 发起上传文件请求
     */
    public final void upload() {
        request(HttpMethod.UPLOAD);
    }

    /**
     * 发起网络请求
     *
     * @param method
     */
    private void request(HttpMethod method) {
        final RestService restService = RestCreator.getRestService();
        Call<String> call = null;

        if (requestCallback != null) {
            requestCallback.onStartRequest();
        }

        if (loaderStyle != null) {
            AtreasureLoader.showLoading(context, loaderStyle);
        }

        switch (method) {
            case GET:
                call = restService.get(url, mParams);
                break;
            case POST:
                call = restService.post(url, mParams);
                break;
            case POST_RAW:
                call = restService.postRaw(url, body);
                break;
            case PUT:
                call = restService.put(url, mParams);
                break;
            case PUT_RAW:
                call = restService.putRaw(url, body);
                break;
            case DELETE:
                call = restService.delete(url, mParams);
                break;
            case UPLOAD:
                final RequestBody requestBody = RequestBody.
                        create(MediaType.parse(MultipartBody.FORM.toString()), uploadFile);
                final MultipartBody.Part body = MultipartBody.Part
                        .createFormData("file", uploadFile.getName(), requestBody);
                call = restService.upload(url, body);
                break;
            default:
                break;
        }

        if (call != null) {
            call.enqueue(new RequestCallbacks(requestCallback,
                    successCallback, errorCallback, failureCallback, loaderStyle, context));
        }

    }

}
